import { across, bet, section, tower } from '@prisma/client'
import { CalcAcrossModel, CalcBetModel } from '../types/model'
import { calcAngle, calcFi, calcH, calcHi, calcPh, calcTi } from './ljMath'

/**
 * xy坐标变换公式
 * @param x x坐标
 * @param y y坐标
 * @returns 新数组
 */
export const scaleXY = (x: number, y: number) => {
  return [x / 5, y * 2]
}

/**
 * 计算档距累加
 * @param bets 塔间集合
 */
export const calcSpan = (towers: tower[], bets: bet[]) => {
  // 定义一个累加档距
  let leiSpan = 0
  // 循环计算tower的累距
  for (const bet of bets) {
    // 将leiSpan添加到属性上
    const [t1] = bet.btName.split('--')
    const tower1 = towers.find((t) => t.tName === t1)
    tower1.leiSpan = leiSpan
    // 档距进行累加
    leiSpan += bet.btSpan
  }

  // 计算最后一基铁塔的
  const [_, t2] = bets[bets.length - 1].btName.split('--')
  const tower2 = towers.find((t) => t.tName === t2)
  tower2.leiSpan = leiSpan
}

/**
 * 计算高差及高差角
 * @param bets 塔间集合
 */
export const calcHandAngle = (towers: tower[], bets: bet[]) => {
  for (const bet of bets) {
    const [t1, t2] = bet.btName.split('--')
    const tower1 = towers.find((t) => t.tName === t1)
    const tower2 = towers.find((t) => t.tName === t2)
    bet.h = calcH(tower1.altitude, tower2.altitude)
    bet.angle = calcAngle(bet.h, bet.btSpan)
  }
}

/**
 * 计算控制点受力
 * @param bets 塔间的集合
 * @param lst_across 控制点集合
 */
export const calcAcrossPower = (towers: tower[], bets: bet[], lst_across: CalcAcrossModel[], w: number, e: number) => {
  for (const across of lst_across) {
    const bet = bets.find((b) => b.id === across.betId)
    const x = across.acrossX
    const y = across.acrossY + across.controlHeight
    const btSpan = bet.btSpan
    const [t1] = bet.btName.split('--')
    const tower1 = towers.find((t) => t.tName === t1)
    const h1 = tower1.altitude
    const h = bet.h
    const angle = bet.angle
    const eh = bet.eh
    const i = bets.indexOf(bet)

    const Hi = calcHi(w, x, btSpan, h1, y, angle)
    const Fi = calcFi(w, btSpan, Hi, angle, h)
    const Ti = calcTi(w, e, i, eh, Hi, Fi)

    across.Hi = Hi
    across.Fi = Fi
    across.Ti = Ti
  }
}

/**
 * 计算出口张力的中间变量
 * @param bets 塔间集合
 * @param e 摩阻系数
 */
export const calceOrBet = (bets: CalcBetModel[], e: number) => {
  let eh = 0
  for (let index = 0; index < bets.length; index++) {
    eh += Math.pow(e, -index) * bets[index].h
    bets[index].eh = eh
  }
}

/**
 * 计算张力整定值
 * @param lst_across 控制点集合
 */
export const calcMaxTi = (lst_across: across[], section: section) => {
  const maxTi = Math.max(...lst_across.map((c) => c.Ti))
  lst_across.map((ac) => (ac.finalControl = ac.Ti === maxTi ? true : false))
  section.maxTi = maxTi
}

/**
 * 计算铁塔的轴向张力
 * @param bets 塔间集合
 * @param section 区段
 * @param w 自重比载
 * @param e 摩阻系数
 */
export const calcTowerPh = (towers: tower[], bets: bet[], section: section, w: number, e: number) => {
  let ti = section.maxTi
  const [t1] = bets[0].btName.split('--')
  const tower1 = towers.find((t) => t.tName === t1)
  tower1.ph = ti
  for (const bet of bets) {
    const h = bet.h
    ti = calcPh(w, ti, h, e)

    const [, t2] = bet.btName.split('--')
    const tower2 = towers.find((t) => t.tName === t2)
    tower2.ph = ti
  }
}

/**
 * 计算铁塔最大的轴向张力
 * @param towers 铁塔集合
 * @param section 区段
 */
export const calcMaxPh = (towers: tower[], section: section) => {
  const maxPh = Math.max(...towers.map((t) => t.ph))
  section.maxPh = maxPh
}
